var info_sentTime;
var info_content;
var info_tab;
var info_model;
var info_cpuUsage;
var info_ramUsage;
var info_intUsage;
var info_extUsage;
var info_battInfo;
var info_battDesc;

function info_init() {
	info_tab = main_createTab("info_content",_T("Info"));
	info_content = document.getElementById("info_content");
	main_contents.push(info_content);
	info_model = document.createElement("h3");
	info_content.appendChild(info_model);
	//CPU
	var cpuBars = document.createElement("div");
	var cpuCaption=info_createCaption();
	info_cpuUsage=new info_CpuUsage(cpuBars,cpuCaption);
	info_content.appendChild(info_createFieldset(_T("CPU"),cpuBars,cpuCaption));
	//RAM
	var ramCanvas = info_createBar();
	var ramBar = new ProgressBar(ramCanvas, true);
	var ramCaption=info_createCaption();
	info_ramUsage = new info_DiskUsage(ramBar, ramCaption);
	info_content.appendChild(info_createFieldset(_T("RAM"),ramCanvas,ramCaption));
	//Internal
	var intCanvas = info_createBar();
	var intBar = new ProgressBar(intCanvas, true);
	var intCaption=info_createCaption();
	info_intUsage = new info_DiskUsage(intBar, intCaption);
	info_content.appendChild(info_createFieldset(_T("InternalStorage"),intCanvas,intCaption));
	//External
	var extCanvas = info_createBar();
	var extBar = new ProgressBar(extCanvas, true);
	var extCaption=info_createCaption();
	info_extUsage = new info_DiskUsage(extBar, extCaption);
	info_content.appendChild(info_createFieldset(_T("ExternalStorage"),extCanvas,extCaption));
	//Battery
	var battCanvas = info_createBar();
	var battBar = new ProgressBar(battCanvas, false);
	var battCaption=info_createCaption();
	info_battUsage = new info_BattInfo(battBar, battCaption);
	info_content.appendChild(info_createFieldset(_T("Battery"),battCanvas,battCaption));
}

function info_createCaption(){
	var caption=document.createElement("div");
	caption.className="caption";
	caption.style.textAlign="right";
	caption.textContent="...";
	return caption;
}

function info_createBar(){
	var canvas = document.createElement("canvas");
	canvas.setAttribute("height", "10px");
	canvas.setAttribute("width", (contentWidth - 20) + "px");
	canvas.appendChild(document.createTextNode(_T("CanvasNotSupported")));
	return canvas;
}

function info_createFieldset(_title,_bars, _caption){
	var fieldset=document.createElement("fieldset");
	var legend=document.createElement("legend");
	legend.appendChild(document.createTextNode(_title));
	fieldset.appendChild(legend);
	fieldset.appendChild(_bars);
	fieldset.appendChild(_caption);
	return fieldset;
}

function info_getInfo() {
	var infoReq = new XMLHttpRequest();
	infoReq.onreadystatechange = function() {
		if (infoReq.readyState == 4 && infoReq.status == 200) {
			addPing(new Date().getTime() - info_sentTime);
			var info=main_readJSONResponse(infoReq);
			if(!info){
				return;
			}
			document.title = info.model;
			info_model.innerHTML = info.model;
			var stat = info.stat;
			info_cpuUsage.update(stat);
			info_ramUsage.update(info.ramTotal, info.ramFree);
			info_extUsage.update(info.extTotal, info.extFree);
			info_intUsage.update(info.intTotal, info.intFree);
			info_battUsage.update(info);
			if (notActivated) {
				notActivated = false;
				main_content.removeChild(loadingMsg);
				main_activateTab(info_content, info_tab);
			}
		}
	};
	main_encryptReq(infoReq,"info");
	info_sentTime = new Date().getTime();
}

function info_CpuStat(_cpuInfo) {
	this.previous = _cpuInfo;
	this.actual = new Array();

	this.compute = function(_cpuInfo) {
		for ( var key in _cpuInfo) {
			if (key != "name") {
				this.actual[key] = _cpuInfo[key] / 1 - this.previous[key] / 1;
			}
		}
		this.previous = _cpuInfo;
	}
}

function info_CpuUsage(_barsField, _caption){
	this.cpuStats=null;
	this.cpuBars=null;
	this.barsField=_barsField;
	this.caption=_caption;
	
	this.update=function(_stat) {
			if (this.cpuStats == null) {
				this.cpuStats = new Array();
				this.cpuBars = new Array();
				for ( var i = 0; i < _stat.length; i++) {
					var cpuStat = new info_CpuStat(_stat[i]);
					this.cpuStats[_stat[i].name] = (cpuStat);
					if (_stat[i].name != "cpu") {
						var canvas=info_createBar();
						this.barsField.appendChild(canvas);
						this.cpuBars[_stat[i].name] = new ProgressBar(canvas, true);
					}
				}
			} else {
				for ( var i = 0; i < _stat.length; i++) {
					var cpuStat = this.cpuStats[_stat[i].name];
					cpuStat.compute(_stat[i]);
					var actual = cpuStat.actual;
					var total = actual.total;
					if (_stat[i].name == "cpu") {
						var userPercent = Math.round((actual.user / total) * 100);
						var systemPercent = Math.round((actual.system / total) * 100);
						var ioPercent = Math.round((actual.io / total) * 100);
						this.caption.innerHTML = _T("User")+": " + userPercent
								+ "%, "+_T("System")+": " + systemPercent + "%, "+_T("IO")+": " + ioPercent
								+ "%";
					} else {
						var usage = actual.user + actual.system + actual.io;
						this.cpuBars[_stat[i].name].draw(total, usage);
					}
				}
			}
	}
}

function info_DiskUsage(_bar, _label) {
	this.bar = _bar;
	this.label = _label;
	this.totalValue = 0;
	this.freeValue = 0;
	this.update = function(_total, _free) {
		this.dirty = false;
		if (this.totalSize == null || this.totalValue != _total) {
			this.totalSize = new Size(_total);
			this.dirty = true;
		}
		if (this.freeSize == null || this.freeValue != _free) {
			this.freeSize = new Size(_free);
			this.dirty = true;
		}
		if (this.dirty) {
			this.occ = _total - _free;
			this.bar.draw(_total, this.occ);
			this.label.innerHTML = _T("Total")+": " + this.totalSize.string()
					+ "; "+_T("Free")+": " + this.freeSize.string();
		}
	};
}

function info_BattInfo(_bar, _desc) {
	this.bar = _bar;
	this.desc = _desc;
	this.update = function(_info) {
		this.bar.draw(_info.battScale, _info.battLevel);
		var status = "???";
		switch (_info.battStatus) {
		case 1:
			status = _T("Charging");
			break;
		case 2:
			status = _T("Discharging");
			break;
		case 3:
			status = _T("Full");
			break;
		case 4:
			status = _T("Idle");
			break;
		}
		var health = _T("Unknown");
		switch (_info.battHealth) {
		case 1:
			health = _T("Cold");
			break;
		case 2:
			health = _T("Dead");
			break;
		case 3:
			health = _T("Good");
			break;
		case 4:
			health = _T("Overvoltage");
			break;
		case 5:
			health = _T("Overheat");
			break;
		case 7:
			health = _T("Failure");
			break;
		}
		var voltage = _info.battU;
		if (voltage > 1000) {
			voltage /= 1000;
		}
		var temperature = _info.battT;
		if (temperature > 100) {
			temperature /= 10;
		}
		this.desc.textContent=_T("Status")+": "+status+"; "+_T("Voltage")+": "+voltage+"V; "+
		_T("Health")+": "+health+"; "+_T("Temp")+": "+temperature+"°C "
	};
}